package com.zp.drug.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.zp.common.core.controller.BaseController;
import com.zp.common.core.entity.R;
import com.zp.common.core.validate.AddGroup;
import com.zp.common.core.validate.EditGroup;
import com.zp.common.excel.utils.ExcelUtil;
import com.zp.common.log.annotation.Log;
import com.zp.common.log.enums.BusinessType;
import com.zp.common.myBatis.core.page.PageQuery;
import com.zp.common.myBatis.core.page.TableDataInfo;
import com.zp.drug.entity.bo.DrugInventoryCheckPreparationBo;
import com.zp.drug.entity.vo.DrugInventoryCheckPreparationVo;
import com.zp.drug.service.IDrugInventoryCheckPreparationService;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotNull;
import java.util.List;

/**
 * 药品预盘点表控制器
 * 前端访问路由地址为:/drug/drugInventoryCheckPreparation
 *
 * @author zhang peng
 * @ date 2024-06-14
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/drugInventoryCheckPreparation")
public class DrugInventoryCheckPreparationController extends BaseController {

    private final IDrugInventoryCheckPreparationService iDrugInventoryCheckPreparationService;

    /**
     * 查询药品预盘点表列表 (分页)
     */
    @SaCheckPermission("drug:drugInventoryCheckPreparation:list")
    @GetMapping("/selectPage")
    public TableDataInfo<DrugInventoryCheckPreparationVo> selectPage(DrugInventoryCheckPreparationBo bo, PageQuery pageQuery) {
        return iDrugInventoryCheckPreparationService.queryPageList(bo, pageQuery);
    }

    /**
     * 按申请单分组查询盘点记录
     */
    @GetMapping("/selectGroupInventoryNo")
    public TableDataInfo<DrugInventoryCheckPreparationVo> selectGroupInventoryNo(DrugInventoryCheckPreparationBo bo, PageQuery pageQuery) {
        return iDrugInventoryCheckPreparationService.selectGroupInventoryNo(bo, pageQuery);
    }

    /**
     * 查询药品预盘点表列表 (不分页)
     */
    @SaCheckPermission("drug:drugInventoryCheckPreparation:list")
    @GetMapping("/list")
    public R<List<DrugInventoryCheckPreparationVo>> list(DrugInventoryCheckPreparationBo bo) {
        List<DrugInventoryCheckPreparationVo> list = iDrugInventoryCheckPreparationService.queryList(bo);
        return R.ok(list);
    }

    /**
     * 导出药品预盘点表列表
     */
    @SaCheckPermission("drug:drugInventoryCheckPreparation:export")
    @Log(title = "药品预盘点表", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(DrugInventoryCheckPreparationBo bo, HttpServletResponse response) {
        List<DrugInventoryCheckPreparationVo> list = iDrugInventoryCheckPreparationService.queryList(bo);
        ExcelUtil.exportExcel(list, "药品预盘点表", DrugInventoryCheckPreparationVo.class, response);
    }

    /**
     * 获取药品预盘点表详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("drug:drugInventoryCheckPreparation:list")
    @GetMapping("/{id}")
    public R<DrugInventoryCheckPreparationVo> getInfo(@NotNull(message = "主键不能为空") @PathVariable String id) {
        return R.ok(iDrugInventoryCheckPreparationService.queryById(id));
    }

    /**
     * 新增药品预盘点表
     */
    @SaCheckPermission("drug:drugInventoryCheckPreparation:add")
    @Log(title = "药品预盘点表", businessType = BusinessType.INSERT)
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody DrugInventoryCheckPreparationBo bo) {
        return toAjax(iDrugInventoryCheckPreparationService.insertByBo(bo) ? 1 : 0);
    }

    /**
     * 修改药品预盘点表
     */
    @SaCheckPermission("drug:drugInventoryCheckPreparation:edit")
    @Log(title = "药品预盘点表", businessType = BusinessType.UPDATE)
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody DrugInventoryCheckPreparationBo bo) {
        return toAjax(iDrugInventoryCheckPreparationService.updateByBo(bo) ? 1 : 0);
    }

    /**
     * 删除药品预盘点表
     *
     * @param ids 主键串
     */
    @SaCheckPermission("drug:drugInventoryCheckPreparation:remove")
    @Log(title = "药品预盘点表", businessType = BusinessType.DELETE)
    @DeleteMapping("/{inventoryNo}/{orgId}")
    public R<Void> remove(@PathVariable String inventoryNo, @PathVariable String orgId) {
        return toAjax(iDrugInventoryCheckPreparationService.deleteWithValidByInventoryNo(inventoryNo, orgId) ? 1 : 0);
    }

    /**
     * 获取盘点药品明细（通过批号等字段分组）
     *
     * @param bo
     * @return
     */
    @PostMapping("/getInventoryCheckList")
    public R<List<DrugInventoryCheckPreparationVo>> getInventoryCheckList(@RequestBody DrugInventoryCheckPreparationBo bo) {
        return R.ok(iDrugInventoryCheckPreparationService.getInventoryCheckList(bo));
    }
}
